
// export function setCookie(c_name,value,expiredays)
// {
//     var exdate = new Date();
//     exdate.setDate(exdate.getDate()+expiredays);
//     //对value值进行编码
//     document.cookie = c_name + "=" + escape(value)+
//     ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
// }

import { ElMessage } from "element-plus";


export function setCookie(cName, value, expiredays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie =
    cName +
    "=" +
    decodeURIComponent(value) +
    (expiredays == null ? "" : ";expires=" + exdate.toGMTString()) +
    ";path=/;domain=.hozonauto.com";
}

// 检查对象是否存在并且是一个对象 
export function isNonEmptyObject(obj) {
  if (obj != null && typeof obj === 'object' && obj.constructor === Object) {
    // 然后检查对象是否有自有属性  
    return Object.keys(obj).length > 0;
  }
  // 如果不满足以上条件，则返回false  
  return false;
}

// 时间格式化 hh:mm:ss
export function formatTime(time) {
  let result = parseInt(time)
  let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  return `${h} : ${m} ：${s}`
}

export function copyToClip(content){
  if(navigator.clipboard && window.isSecureContext){
    ElMessage.success('拷贝成功')
    return navigator.clipboard.writeText(content)
  }else {
    let textArea = document.createElement('textarea')
    textArea.value = content
    textArea.style.position = 'absolute'
    textArea.style.opacity = 0
    textArea.style.left= '-99999px'
    textArea.style.top= '-99999px'
    document.body.appendChild(textArea)
    textArea.focus()
    textArea.select()
    ElMessage.success('拷贝成功')
    return new Promise((res,rej) => {
      document.execCommand('copy') ? res() : rej()
      textArea.remove()
    })
  }
}
  // 4位数随机字母

  export function generateRandomLetters(length){
    let result = ''
    const characters = 'abcdefghijklmnopqrstuvwxyz';
    const charactersLength = characters.length;
    for (let i = 0; i< length;i++){
        result += characters.charAt(Math.floor(Math.random() * charactersLength))
    }
    return result;
  }
// 时间戳：1637244864707
/* 时间戳转换为时间 */
export function timestampToTime(timestamp) {
  timestamp = timestamp ? timestamp : null;
  let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}